<template>
  <div class="app-container">  
      <el-form ref="filterForm" :model="formData" :rules="rules" label-position="left" label-width="100px">
        <el-form-item label="专业编码" prop="majorCode">
          <el-input v-model="formData.majorCode" clearable placeholder="专业编码" maxlength="20" show-word-limit/>
          </el-form-item>
         <el-form-item label="专业名称" prop="majorName">
          <el-input v-model="formData.majorName" clearable placeholder="专业名称" maxlength="80" show-word-limit/>
        </el-form-item>
        <el-form-item label="修学年限" prop="majorDuration">
          <el-input v-model="formData.majorDuration" clearable placeholder="修学年限" maxlength="20" show-word-limit/>
          </el-form-item>
        <el-form-item label="平均薪资" prop="majorSalary">
          <el-input v-model="formData.majorSalary" clearable placeholder="平均薪资" maxlength="20" show-word-limit/>
          </el-form-item>
        <el-form-item label="专业介绍" prop="majorContent">
          <el-input type="textarea" v-model="formData.majorContent" maxlength="200" show-word-limit clearable placeholder="专业介绍"/>
          </el-form-item>
        <el-form-item label="就业方向" prop="majorJob">
          <el-input type="textarea" v-model="formData.majorJob" maxlength="200" show-word-limit clearable placeholder="就业方向"/>
          </el-form-item>
        <el-form-item label="开设课程" prop="majorCourse">
          <el-input type="textarea" v-model="formData.majorCourse" maxlength="200" show-word-limit clearable placeholder="开设课程"/>
          </el-form-item>
        <el-form-item label="相关证书" prop="majorCertificate">
          <el-input type="textarea" v-model="formData.majorCertificate" maxlength="200" show-word-limit clearable placeholder="相关证书"/>
          </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" v-model="formData.remark" maxlength="200" show-word-limit clearable placeholder="备注"/>
        </el-form-item>
      </el-form>
      <el-row :gutter="20">
        <el-col :span="14">          
            <el-button type="info" @click="onFormSaveButton()">保存</el-button>
            <el-button type="primary" @click="onFormSaveAndNewButton()">保存并新建</el-button>
            <el-button type="primary" @click="onFormNewButton()">新建</el-button>
        </el-col>
        <el-col :span="6" style="text-align:right;">          
          <el-button type="warning" @click="onFormCannelButton()">取消</el-button>
          <el-button type="danger" v-if="majorID > 0" @click="onFormRemoveButton()">删除</el-button>
        </el-col>
      </el-row>

  </div>
</template>

<script>
import waves from '@/directive/waves' // waves directive
import Region from '@/components/Region/RegionSimple'
import Upload from '@/components/Upload/SingleImage3'
import {getMajorDetail,saveMajor,removeMajor} from '@/api/knowledge/major'



export default {
  name: 'majorForm',
  components: { Region,Upload },
  directives: { waves },
  filters: {
  },
  data() {
    return {
      majorID:undefined,
      exchangeOptions:[],
      formData: {
      },
      rules:{
         majorName: [
            { required: true, message: '请输入专业名称', trigger: 'blur' },
            { min: 2, max: 80, message: '长度在 3 到 80 个字符', trigger: 'blur' }
          ]
      }
    }
  },
  created() {
    let majorID = this.$route.query.majorID 
    majorID = parseInt(majorID)
    if(isNaN(majorID) == false){
      this.majorID = majorID
      this.getInfo()
    }
    let parentMajorID = this.$route.query.parentMajorID
    parentMajorID = parseInt(parentMajorID)
    console.log(isNaN(parentMajorID))
    if(isNaN(parentMajorID) == false){
      this.formData.parentMajorID = parentMajorID
    }
  },
  methods: {
    getInfo:function(){
      let _this = this;
      getMajorDetail({
        "majorID":this.majorID
      }).then((response) => {
        console.log("getList",response)
          if (response.code === 200) {
            this.formData = response.data
          } else {
            _this.$message({
              message: response.msg,
              type: 'error'
            })
          }
      })
    },
    onFormSaveButton:function(){
      let _this = this;
      this.$refs['filterForm'].validate((valid) => {
        if (valid) {
          if(_this.majorID != undefined){
            _this.formData['majorID'] = this.formData.ID
          }
            saveMajor(_this.formData).then((response) => {
              console.log("getList",response)
                if (response.code === 200) {
                  _this.$message({
                    message: "成功",
                    type: 'error'
                  })
                } else {
                  _this.$message({
                    message: response.msg,
                    type: 'error'
                  })
                }
            })
        } else {
            console.log('error submit!!');
            return false;
        }
      })
    },
    onFormSaveAndNewButton:function(){
      let _this = this;
        if(_this.majorID != undefined){
          _this.formData['majorID'] = this.formData.ID
        }
      saveMajor(_this.formData).then((response) => {
          if (response.code === 200) {
            _this.onFormNewButton()
          } else {
            _this.$message({
              message: response.msg,
              type: 'error'
            })
          }
      })
    },
    onFormNewButton:function(){
        this.formData = {

        }
    },
    onFormCannelButton:function(){
      this.$router.push({ path: 'major'})
    },
    onFormRemoveButton:function(){
      let _this = this;
      removeMajor({
        "majorID":this.majorID
      }).then((response) => {
        console.log("getList",response)
          if (response.code === 200) {
            _this.onFormCannelButton()
          } else {
            _this.$message({
              message: response.msg,
              type: 'error'
            })
          }
      })
    }
  }
}
</script>
<style>
</style>